package cn.scxy.bookcity.service.impl;

import cn.scxy.bookcity.ex.ServiceException;
import cn.scxy.bookcity.mapper.CategoryMapper;
import cn.scxy.bookcity.pojo.vo.CategoryListVO;
import cn.scxy.bookcity.service.ICategoryService;
import cn.scxy.bookcity.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class CategoryServiceImpl implements ICategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public List<CategoryListVO> list(Long id) {
        List<CategoryListVO> lists = categoryMapper.list(id);
        System.out.println("-------" + lists);
        return lists;
    }

    @Override
    public List<CategoryListVO> list1(Long id) {
        List<CategoryListVO> categoryListVOS = categoryMapper.list1(id);
        return categoryListVOS;
    }

    @Override
    public void deleteById(Long id) {
        List<CategoryListVO> list = categoryMapper.list(id);

        if (list.size()!=0) {
            throw new ServiceException(ServiceCode.ERR_DELETE, "删除失败!");
        }
        int rows = categoryMapper.deleteById(id);
        if (rows != 1) {
            throw new ServiceException(ServiceCode.ERR_CONFLICT, "要删除的类别不存在");
        }


    }

    @Override
    public void updateEnable(Long id) {
        categoryMapper.updateEnable(id);
    }

    @Override
    public void updateDisable(Long id) {
        categoryMapper.updateDisable(id);
    }
}
